package com.tfjybj.itoo.exam.provider.controller;

import org.apache.dubbo.config.annotation.Reference;;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.provider.service.BuildingService;
import com.tfjybj.itoo.teach.FacadeApi.BuildingApi;
import com.tfjybj.itoo.teach.entity.BuildingEntity;
import com.tfjybj.itoo.teach.model.BuildingModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * BuildingController
 * building表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-11-15 10:35:56
 */
@Api(tags = {"building表接口"})
@RequestMapping(value = "/building")
@RestController
@Slf4j
public class BuildingController {

    @Resource
    private BuildingService buildingService;

    @Reference(check = false)
    private BuildingApi buildingApi;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model BuildingModel
     * @return 添加的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody com.tfjybj.itoo.teach.model.BuildingModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        com.tfjybj.itoo.teach.entity.BuildingEntity buildingEntity = new com.tfjybj.itoo.teach.entity.BuildingEntity();
        BeanUtils.copyProperties(model, buildingEntity);
        buildingApi.create(buildingEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        buildingApi.delete(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        buildingApi.deleteByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model BuildingModel
     * @return 修改后的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id修改building")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody BuildingModel model) {
        if (StringUtils.isEmpty(model.getName())) {
            return ItooResult.build(ItooResult.FAIL, "name为空");
        }
        if (StringUtils.isEmpty(model.getCode())) {
            return ItooResult.build(ItooResult.FAIL, "code为空");
        }
        com.tfjybj.itoo.teach.entity.BuildingEntity buildingEntity = new com.tfjybj.itoo.teach.entity.BuildingEntity();
        BeanUtils.copyProperties(model, buildingEntity);
        buildingApi.modify(buildingEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找Building
     *
     * @param id 主键id
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        com.tfjybj.itoo.teach.entity.BuildingEntity buildingEntity = buildingApi.findById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", buildingEntity);
    }

    /**
     * 分页查询所有Building
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "分页查询所有Building")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<com.tfjybj.itoo.teach.entity.BuildingEntity> buildings = buildingApi.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", buildings);
    }

    /**
     * 根据建筑名称模糊查询building
     *
     * @param name     建筑名称
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的building
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "根据建筑名称模糊查询building", notes = "分页根据建筑名称模糊查询building")

    @GetMapping(value = "queryBuildingByLikeName/{pageNo}/{pageSize}")
    public ItooResult queryBuildingByLikeName(@RequestParam(required = false, defaultValue = "") String name,
                                              @ApiParam(value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                              @ApiParam(value = "记录数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<com.tfjybj.itoo.teach.entity.BuildingEntity> buildingList = buildingApi.queryBuildingByLikeName(name, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", buildingList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据id查找Building信息包含校区
     *
     * @return buildingEntity
     * @author 杨江涛
     * @since 2.0.0 2018-11-15 10:35:56
     */
    @ApiOperation(value = "无参查询建筑信息（包含校区）")
    @GetMapping(value = {"/findBuilding"})
    public ItooResult findBuilding() {
        List<com.tfjybj.itoo.teach.entity.BuildingEntity> buildingEntity = buildingApi.findBuilding();
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", buildingEntity);
    }

    /**
     * logWarnTest
     *
     * @param id 主键id
     * @author 李坤
     * @since 2.0.0 2018年12月31日10:15:53
     */
    @ApiOperation(value = "logWarnTest")
    @GetMapping(value = {"/logWarnTest/{id}"})
    public ItooResult logWarnTest(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        log.warn("warn测试"+id);
        log.error("error测试"+id);
        com.tfjybj.itoo.teach.entity.BuildingEntity buildingEntity = buildingApi.findById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", buildingEntity);
    }

}    
